import unittest

def setUpModule():
    print('测试模块开始')

def tearDownModule():
    print('测试模块结束')


class TestMethod(unittest.TestCase):
    def setUp(self):
        print("test start")
    
    @classmethod
    def setUpClass(cls):
        print('测试类开始')

    @classmethod
    def tearDownClass(cls):
        print('测试类结束')

    @unittest.skip('1')
    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO', '测试是否相等')
        self.assertNotEqual('Foao'.upper(), 'FOO', '测试是否相等')
        self.assertTrue('FOO'.isupper())
        self.assertFalse('foo'.isupper())
        self.assertIn(1, [1,2])
        self.assertIn(1, (1,23,))
        self.assertIn(1, range(1,10))
        self.assertNotIn(10, range(1,10))
        self.assertIn(9, range(1,10))
        self.assertIs(3, 3)
        self.assertIsNot(1,2)   
        self.assertIsNone(None)
        self.assertIsNotNone(1)
        self.assertIsInstance(int, type)
        self.assertIsInstance(float, type)
        self.assertNotIsInstance(1, type)
        self.assertAlmostEqual(1.999, 2.000, 2)
        self.assertAlmostEqual(1.99999999, 2.0)
        self.assertNotAlmostEqual(1.9999999, 2.0) # 默认是七位
        self.assertAlmostEqual(1.999, 2.000, 2)
        self.assertNotAlmostEqual(1.999, 2.000, 3)
        self.assertAlmostEqual(1.999, 2.000, None, None, 0.001)
        self.assertNotAlmostEqual(1.99, 2.000, None, None, 0.001)

    @unittest.skip('cup物品')
    def test_compare(self):
        self.assertGreater(2,1)
        self.assertGreaterEqual(2,2)
        self.assertLess(2,5)
        self.assertLessEqual(5,5)

    @unittest.skipIf(False,'')
    def test_regex(self):
        self.assertRegex('1234', '\d+')
        self.assertRegex('1234', '\d')
        self.assertNotRegex('1234', '\s')

    @unittest.skipUnless(True, '')
    def test_somes_equal(self):
        self.assertListEqual([1,2], [1,2])
        self.assertTupleEqual((1,2,3), (1,2,3))
        self.assertDictEqual({'name':'wyl350'}, {'name':'wyl350'})
        self.assertSetEqual({'name'},{'name'})

    @unittest.expectedFailure
    def test_demo1(self):
        self.assertEqual(1,2)
        pass

    def tearDown(self):
        print("test end")

def clearUp():
    print('清理函数')
    pass

class TestMethod2(unittest.TestCase):
    def setUp(self) -> None:
        self.addCleanup(clearUp)

    def test_a(self):
        # self.addCleanup(clearUp)
        self.assertEqual(1,1)
        # self.doCleanups()
        pass

    def tearDown(self) -> None:
        self.doCleanups()
        pass
    pass

    # @unittest.expectedFailure
    def test_b(self):
        data = [1,2,3,5,6]
        for i in data:
            with self.subTest(data=i):
                self.assertNotEqual(1, i, '判断预期结果')


if __name__ == "__main__":
    unittest.main()
